<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'电客师排名'),
);
//客户列表
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('juese'=>5,'status'=>1,'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
		$yesyue = mktime(0,0,0,date('m')-1,1,date('Y'));
		$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
		if($_RQ['createtime']){
			$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
			$time_range=json_decode($time_range,true);
			$return['params']['createtime']=$time_range;
			$params['jiao_time between']=array($time_range['tstart'],$time_range['tend']);
		}
		foreach($list as &$item){
			$params['worker5id']=$item['id'];
			$params['type']=0;
			$item['fee1']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$params['type']=1;
			$item['fee2']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$params['type']=2;
			$item['fee3']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$params['type']=3;
			$item['fee4']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$params['type']=4;
			$item['fee5']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$item['allfee']=$item['fee1']+$item['fee2']+$item['fee3']-$item['fee4']-$item['fee5'];
			$item['now']=pdo_getsum('jy_yxw_feelog',array('type'=>array(0,1,2),'uniacid'=>$_SESSION['uniacid'],'worker5id'=>$item['id'],'status'=>1,'jiao_time >'=>$nowyue),'fee');
			$item['yes']=pdo_getsum('jy_yxw_feelog',array('type'=>array(0,1,2),'uniacid'=>$_SESSION['uniacid'],'worker5id'=>$item['id'],'status'=>1,'jiao_time between'=>array($yesyue,$nowyue)),'fee');
		}
	}
	$heji=array('title'=>'合计');
	paixu($list,'allfee','desc');
	foreach($list as $k=>&$v){
		$v['rank']=$k+1;
		$heji['fee2'] +=$v['fee2'];
		$heji['fee1'] +=$v['fee1'];
		$heji['fee5'] +=$v['fee5'];
		$heji['fee4'] +=$v['fee4'];
		$heji['fee3'] +=$v['fee3'];
		$heji['allfee'] +=$v['allfee'];
		$heji['now'] +=$v['now'];
		$heji['yes'] +=$v['yes'];
	}
	$list[]=$heji;
	$return['list']=$list;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'姓名','type'=>'string'),
			array('name'=>'rank','title'=>'排名','type'=>'string'),
			array('name'=>'storeid','title'=>'门店','type'=>'select2','options'=>'stores'),
			array('name'=>'fee2','title'=>'定金','type'=>'string'),
			array('name'=>'fee1','title'=>'单费','type'=>'string'),
			array('name'=>'fee3','title'=>'分期缴费','type'=>'string'),
			array('name'=>'fee5','title'=>'退定金','type'=>'string'),
			array('name'=>'fee4','title'=>'退单费','type'=>'string'),
			array('name'=>'allfee','title'=>'实收合计','type'=>'string'),
			array('name'=>'now','title'=>'销售业绩','type'=>'string'),
			array('name'=>'yes','title'=>'上月业绩','type'=>'string'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'电客师名称','name'=>'title','help'=>''),
			array('name'=>'storeid','width'=>'15','title'=>'门店','type'=>'select','options'=>'stores'),
			array('name'=>'createtime','width'=>15,'title'=>'时间段','type'=>'time_range'),
		),
	);
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	//客户列表
	$return['params']=array();
	$params=array('juese'=>5,'status'=>1,'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getall('jy_yxw_worker',$params);
	if($list){
		$nowyue = mktime(0,0,0,date('m'),1,date('Y'));
		$yesyue = mktime(0,0,0,date('m')-1,1,date('Y'));
		$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
		if($_RQ['jiao_day']){
			$params['jiao_day']=$_RQ['jiao_day'];
		}
		if($_RQ['createtime']){
			$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
			$time_range=json_decode($time_range,true);
			$return['params']['createtime']=$time_range;
			$params['jiao_time between']=array($time_range['tstart'],$time_range['tend']);
		}
		foreach($list as &$item){
			$params['worker5id']=$item['id'];
			$params['type']=0;
			$item['fee1']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$params['type']=1;
			$item['fee2']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$params['type']=2;
			$item['fee3']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$params['type']=3;
			$item['fee4']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$params['type']=4;
			$item['fee5']=pdo_getsum('jy_yxw_feelog',$params,'fee');
			$item['allfee']=$item['fee1']+$item['fee2']+$item['fee3']-$item['fee4']-$item['fee5'];
			$item['now']=pdo_getsum('jy_yxw_feelog',array('type'=>array(0,1,2),'uniacid'=>$_SESSION['uniacid'],'worker5id'=>$item['id'],'status'=>1,'jiao_time >'=>$nowyue),'fee');
			$item['yes']=pdo_getsum('jy_yxw_feelog',array('type'=>array(0,1,2),'uniacid'=>$_SESSION['uniacid'],'worker5id'=>$item['id'],'status'=>1,'jiao_time between'=>array($yesyue,$nowyue)),'fee');
		}
	}
	$heji=array('title'=>'合计');
	paixu($list,'allfee','desc');
	foreach($list as $k=>&$v){
		$v['rank']=$k+1;
		$heji['fee2'] +=$v['fee2'];
		$heji['fee1'] +=$v['fee1'];
		$heji['fee5'] +=$v['fee5'];
		$heji['fee4'] +=$v['fee4'];
		$heji['fee3'] +=$v['fee3'];
		$heji['allfee'] +=$v['allfee'];
		$heji['now'] +=$v['now'];
		$heji['yes'] +=$v['yes'];
	}
	$list[]=$heji;
	$header=array(
		'title'=>'姓名',
		'rank'=>'排名',
		'storeid'=>'门店',
		'fee2'=>'定金',
		'fee1'=>'单费',
		'fee3'=>'分期缴费',
		'fee5'=>'退定金',
		'fee4'=>'退单费',
		'allfee'=>'实收合计',
		'now'=>'销售业绩',
		'yes'=>'上月业绩',
	);
	model('core/tools')->export2excel($header,$list,'电客师排名数据');
}